package onehour.kafka.example.dhy;

import onehour.kafka.example.serialization.AvroSerializer;
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.clients.producer.RecordMetadata;
import org.apache.kafka.common.serialization.StringSerializer;

import java.util.Properties;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

import static onehour.kafka.example.dhy.KafkaConstants.KAFKA_ADDRESS;

public class DhyProducer {
    public static void main(String[] args) {
        Properties props = new Properties();
        props.put("bootstrap.servers",KAFKA_ADDRESS);
        props.put("acks","1");
        props.put("retries","3");
        props.put("key.serializer", StringSerializer.class.getName());
        props.put("value.serializer", StringSerializer.class.getName());

        try (KafkaProducer<String, String> producer = new KafkaProducer<>(props)) {
            for (int i = 0; i < 20; i++) {
                //同步发送--发送完一条消息后,才能发送下一条消息
                producer.send(new ProducerRecord<String, String>("my-topic", Integer.toString(i), Integer.toString(i)));
                Thread.sleep(1000);
            }
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }
}
